/**
 * @Author: colpu
 * @Date: 2025-03-29 19:38:16
 * @LastEditors: colpu ycg520520@qq.com
 * @LastEditTime: 2025-10-21 17:10:07
 * @
 * @Copyright (c) 2025 by colpu, All Rights Reserved.
 */
import is from "is-type-of";
import { join } from "node:path";
import debugFactory from "debug";
const debug = debugFactory("core:loader/mixin/controller");

import { wrapClass, addDirectory } from "../../utils/index.js";
export default async function loadController(ctx) {
  ctx.timing.time("加载controller时间");
  const directory = [join(ctx.root, "src/controller")];
  directory.push(...addDirectory(join(ctx.root, "src/modules"), 'controller'));
  const options = {
    caseStyle: "lower",
    directory,
    iscall: false,
    initializer: (obj, opt) => {
      debug("load controller initializer obj %o", opt);
      if (is.class(obj)) {
        obj.prototype.pathname = opt.pathname;
        obj.prototype.fullpath = opt.fullpath;
        return wrapClass(obj);
      } else {
        return obj;
      }
    },
  };
  await ctx.loadToApp(directory, "controller", options);
  debug("loadController: %o", directory, options);
  ctx.timing.timeEnd("加载controller时间");
}
